Golang : Get current time
Problem :
Need to get current time stamp in Go and display in string.
Solution :
Use the time.Local() and time.Format() functions.
getcurrenttime.go
package main
import (
"fmt"
"os"
"time"
)
func main() {
// get current timestamp
currenttime := time.Now().Local()
fmt.Println("Current time : ", currenttime.Format("2006-01-02 15:04:05 +0800"))
}
Execute the code
>go run getcurrenttime.go
and the output will be :
Current time : 2014-06-30 14:23:38 +0800
Take note that +0800
is the timezone in my current location. You can change it to suit your timezone.
Reference :
See also : Golang : Get file last modified date and time
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.3k Generate Random number with math/rand in Go
+21.6k Golang : Calculate time different
+6.6k Golang : Shuffle strings array
+19.7k Golang : How to read float value from standard input ?
+16.2k Golang : Clone with pointer and modify value
+7.1k Golang : Variadic function arguments sanity check example
+14.2k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+12.9k Golang : Get uploaded file name or access uploaded files
+7.9k Golang : Get SPF and DMARC from email headers to fight spam
+8.3k Golang : Terminate-stay-resident or daemonize your program?
+40.2k Golang : Convert string to array/slice
+7.1k Prevent Write failed: Broken pipe problem during ssh session with screen command